/*
 * Copyright (c) 2022-2023 cetcclout Co., Ltd All rights reserved.
 * Sensor Adapter is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.cetccloud.cimp.common.util;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.date.DateTime;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.Tables;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import java.io.FileNotFoundException;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 
 * @apiNote 
 * @date 2023/3/21
 */
@Component
public class DataConfig {

    /**
     * 随机数据集
     */
    public static Map<String, List<Map<String, Object>>> mapData = new ConcurrentHashMap<>();
    /**
     * 气象数据
     * datetime: row
     */
    public static Map<String, Object> qxMapData = new ConcurrentHashMap<>();

    /**
     * 大气6项 2023-07-13
     * datetime: row
     */
    public static Map<String, Object> airMapData = new ConcurrentHashMap<>();

    /**
     * 模拟设备链接对象集
     */
    public static Map<String, Socket> CONN_MAP = new ConcurrentHashMap<>();

    public static Table<String, String, List<Double>> MINUTE_DATA = Tables.synchronizedTable(HashBasedTable.create());

    public static Table<String, String, List<Double>> HOUR_DATA = Tables.synchronizedTable(HashBasedTable.create());

    /**
     *
     * I21	地表水
     * I22	大气
     * I23	声
     * I24	地下水
     * I25	土壤
     * I26	海水
     * I27	挥发性有机物
     * I31	废气
     * I32	废水
     * I35	土壤环境污染源
     * I36	噪声
     * I37	振动环境污染源
     * I38	放射性环境污染源
     * I39	扬尘
     * I40	固废
     * I41	电磁环境污染源
     * I51	烟气排放过程监控
     * I52	污水排放过程监控
     *
     * */
    public void getData(){
        try {
            ImportParams params = new ImportParams();
            //AIR
            List<Map<String, Object>> listAir =
                    ExcelImportUtil.importExcel(ClassPathResource.class.getClassLoader().getResourceAsStream("excel/air.xlsx"), Map.class, params);
            mapData.put("I22",listAir);

            List<Map<String, Object>> listWater =
                    ExcelImportUtil.importExcel(ClassPathResource.class.getClassLoader().getResourceAsStream("excel/water.xlsx"), Map.class, params);
            mapData.put("I21",listWater);

            //废气
            List<Map<String, Object>> listWasteGas =
                    ExcelImportUtil.importExcel(ClassPathResource.class.getClassLoader().getResourceAsStream("excel/wasteGas.xlsx"), Map.class, params);
            mapData.put("I31",listWasteGas);

            //废水
            List<Map<String, Object>> listWasteWater =
                    ExcelImportUtil.importExcel(ClassPathResource.class.getClassLoader().getResourceAsStream("excel/wasteWater.xlsx"), Map.class, params);
            mapData.put("I32",listWasteWater);

            //气象
            List<Map<String, Object>> listQiXiang =
                    ExcelImportUtil.importExcel(ClassPathResource.class.getClassLoader().getResourceAsStream("excel/qixiangJt.xlsx"), Map.class, params);
            //地面气压 上下浮动0.002
            //瞬时风向 不超过0-360
            listQiXiang.stream().forEach(x->{
                setExcelByDateTimeMap(x,qxMapData);
            });
            //大气六项 2023-07-13
            List<Map<String, Object>> airSixList =
                    ExcelImportUtil.importExcel(ClassPathResource.class.getClassLoader().getResourceAsStream("excel/airSix.xlsx"), Map.class, params);
            airSixList.stream().forEach(x -> {
                setExcelByDateTimeMap(x,airMapData);
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void setExcelByDateTimeMap(Map<String, Object> excelRow,Map<String, Object> dataContainer){
        if (excelRow != null) {
            Object timeCell = excelRow.get("时间");
            if(timeCell!=null) {
                String time = timeCell.toString().replace("‘", "").replace("/", "-");
                DateTime xlsDateTime = new DateTime(time, "yyyy-MM-dd HH");
                String xlsYear = xlsDateTime.year() + "";
                //将excel中的年替换成空 -MM-dd HH
                String xlsDateStrKey = xlsDateTime.toString().replace(xlsYear, "");
                dataContainer.put(xlsDateStrKey,excelRow);
            }
        }
    }
}
